package com.itheima.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "/TimeServlet", urlPatterns = "/time")
public class TimeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd/hh:mm:ss");
        final String format1 = format.format(date);
        System.out.println(format1);

        for (int i = 0; i < 50; i++) {
            Cookie cookie = new Cookie("time" + i, format1);
            cookie.setMaxAge(60 * 60 * 24);

            cookie.setPath("/day04");


            response.addCookie(cookie);
        }


        final Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie1 : cookies) {
                //if ("time".equals(cookie1.getName())){
                response.getWriter().write(cookie1.getValue());
                response.getWriter().write("\r\n");
                //}
            }
        }
    }
}
